package com.jiangyg.mall.core.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * Jackson工具类
 *
 * @author jiangyg
 */
@SuppressWarnings("unchecked")
public class JsonUtils {

    private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);

    /**
     * 默认的ObjectMapper
     */
    private static final ObjectMapper defaultMapper = new ObjectMapper();

    static {
        defaultMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        defaultMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 空值不序列化
        defaultMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        // TODO 支持的时间太少
        defaultMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 功能描述：将java对象转换成json字符串
     *
     * @param obj java对象
     * @return JSON字符串
     */
    public static String toJSONString(Object obj) {
        try {
            return defaultMapper.writeValueAsString(obj);
        } catch (Exception ex) {
            logger.error("对象转换成json字符串出错：", ex);
        }
        return null;
    }

    /**
     * 功能描述：将json字符串转换成java对象
     *
     * @param json  json字符串
     * @param clazz java对象Clazz
     * @return bean
     */
    public static <T> T parseObject(String json, Class<T> clazz) throws Exception {
        return defaultMapper.readValue(json, clazz);
    }

    /**
     * 功能描述：将流转换成java对象
     *
     * @param ins   流
     * @param clazz java对象Clazz
     * @return bean
     */
    public static <T> T parseObject(InputStream ins, Class<T> clazz) throws Exception {
        return defaultMapper.readValue(ins, clazz);
    }

    /**
     * 功能描述：将字节流转换成java对象
     *
     * @param bytes 字节流
     * @param clazz java对象Clazz
     * @return bean
     */
    public static <T> T parseObject(byte[] bytes, Class<T> clazz) throws Exception {
        return defaultMapper.readValue(bytes, clazz);
    }

    /**
     * 功能描述：将json字符串转换成java对象
     *
     * @param json json字符串
     * @param type 类型
     * @return bean
     */
    public static <T> T parseObject(String json, TypeReference<T> type) throws Exception {
        return defaultMapper.readValue(json, type);
    }

    /**
     * 功能描述：将json字符串转换成数组
     *
     * @param json json字符串
     * @param type 类型
     * @return 数组
     */
    public static <T> T[] parseArray(String json, TypeReference<T[]> type) throws Exception {
        return defaultMapper.readValue(json, type);
    }

    /**
     * 功能描述：将json字符串转换成Map集合
     *
     * @param json json字符串
     * @return Map
     */
    public static Map<String, Object> parseMap(String json) throws Exception {
        return defaultMapper.readValue(json, Map.class);
    }


}